package ZguardSuspension;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.Hashtable;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @author： pang-yun
 * @date： 2021-11-01 14:55
 * 功能目的 ：用于
 */

public class MailBB {
    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 2; i++) {
            new People().start();
        }
        TimeUnit.SECONDS.sleep(2);
        // 送信
        for (Integer i : MailBoxes.getIds()) {
            new PostMan(i, "content: " + i).start();
        }
    }
}


@Slf4j
class People extends Thread {
    @Override
    public void run() {
        //收信
        GuardObject guardObject = MailBoxes.creatGuardObject();
        log.info("开始收信！ {}", guardObject.getId());
        Object o = guardObject.get(5000);
        log.info("内容是： {}", o);
    }
}

@Slf4j
class PostMan extends Thread {
    private int id;
    private String mail;

    public PostMan(int id, String mail) {
        this.id = id;
        this.mail = mail;
    }

    @Override
    public void run() {
        GuardObject guardObject = MailBoxes.getGuardObject(id);
        log.info("开始送信 id: {}", guardObject.getId());
        guardObject.complete(mail);
        log.info("内容是 : {}", mail);
    }
}

class MailBoxes {
    private static Map<Integer, GuardObject> boxes = new Hashtable();

    private static int id = 1;

    // 产生唯一 id
    private static synchronized int generator() {
        return id++;
    }

    // 创建
    public static GuardObject creatGuardObject() {
        int generator = generator();
        GuardObject guardObject = new GuardObject(generator);
        boxes.put(generator, guardObject);
        return guardObject;
    }


    // 所有的id
    public static Set<Integer> getIds() {
        return boxes.keySet();
    }


    // 根据 id  找到 邮箱
    public static GuardObject getGuardObject(int id) {
        return boxes.remove(id);
    }
}


@Data
class GuardObject {
    private int id;
    //结果
    private Object response;

    public GuardObject(int id) {
        this.id = id;
    }

    // 获取结果
    // 超时等待  如果超过时间将 不再等待！
    public Object get(long timeout) {
        synchronized (this) {
            // 开始时间
            long begin = System.currentTimeMillis();
            // 已经等待的时间！
            long passTime = 0;
            while (response == null) {
                if (passTime >= timeout) {
                    break;
                }
                try {
                    this.wait(timeout - passTime);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                // 经历时间
                passTime = System.currentTimeMillis() - begin;
            }
            return response;
        }
    }

    // 产生结果
    public void complete(Object response) {
        synchronized (this) {
            // 给成员  变量赋值
            this.response = response;
            this.notifyAll();
        }
    }
}